package com.yc.ui.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.utils.MailUtils;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

public class UpdatePwdDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text_mail;
	private Text text_pwd;
	private Text text_yzm;
	protected String mail;
	private Text text_pwd_1;
	private String pwd;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public UpdatePwdDialog(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(505, 399);
		shell.setText(getText());
		//界面居中显示
		SWTUtil.centerWindow(shell);
		Label lab_mail = new Label(shell, SWT.NONE);
		lab_mail.setBounds(61, 67, 76, 20);
		lab_mail.setText("邮箱");
		
		Label lab_tishi_mail = new Label(shell, SWT.NONE);
		lab_tishi_mail.setBounds(171, 92, 187, 20);
		
		text_mail = new Text(shell, SWT.BORDER);
		text_mail.setBounds(171, 64, 187, 26);
		text_mail.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				mail = text_mail.getText().trim();
				//判空
				if(StringUtil.checkNull(mail)) {
					return;
				}							
				//判空
				if(MailUtils.isEmail(mail)) {
					lab_tishi_mail.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
					lab_tishi_mail.setText("邮箱格式正确");
				}else {
					lab_tishi_mail.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
					lab_tishi_mail.setText("邮箱格式错误");
				}
			}
		});
		
		Label lab_pwd = new Label(shell, SWT.NONE);
		lab_pwd.setBounds(61, 121, 76, 20);
		lab_pwd.setText("密码");
		
		Label lab_tishi_pwd = new Label(shell, SWT.WRAP);
		lab_tishi_pwd.setBounds(171, 146, 187, 20);
		
		text_pwd = new Text(shell, SWT.BORDER);
		text_pwd.setBounds(171, 118, 187, 26);
		text_pwd.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				pwd = text_pwd.getText().trim();
				//判空
				if(StringUtil.checkNull(pwd)) {
					return;
				}							
				//可以包含数字、字母、下划线，并且要同时含有数字和字母，且长度要在6-15位之间
				if(MailUtils.isPwd(pwd)) {
					lab_tishi_pwd.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
					lab_tishi_pwd.setText("密码格式正确");
				}else {
					lab_tishi_pwd.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
					lab_tishi_pwd.setText("密码格式错误");
				}
			}
		});
		Label lab_yzm = new Label(shell, SWT.NONE);
		lab_yzm.setBounds(61, 230, 76, 20);
		lab_yzm.setText("验证码");
		
		text_yzm = new Text(shell, SWT.BORDER);
		text_yzm.setBounds(171, 227, 118, 26);
		
		Button But_update = new Button(shell, SWT.NONE);
		But_update.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String yzm = text_yzm.getText().trim();
				if(yzm.equals(MailUtils.verifyCode)) {
					System.out.println(111);
				}
			}
		});
		But_update.setBounds(105, 291, 98, 30);
		But_update.setText("修改密码");
		
		Button But_dis = new Button(shell, SWT.NONE);
		But_dis.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		But_dis.setBounds(304, 291, 98, 30);
		But_dis.setText("取消");
		
		Button But_mail = new Button(shell, SWT.NONE);
		But_mail.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(MailUtils.isEmail(mail)) {
					MailUtils.sendMail(mail);
				}
			}
		});
		But_mail.setBounds(364, 62, 98, 30);
		But_mail.setText("发送验证码");
		
		Label lblNewLabel = new Label(shell, SWT.WRAP);
		lblNewLabel.setBounds(364, 101, 98, 65);
		lblNewLabel.setText("含有数字和字母，长度在6-15位之间");
		
		Label lab_pwd_1 = new Label(shell, SWT.NONE);
		lab_pwd_1.setText("确认密码");
		lab_pwd_1.setBounds(61, 172, 76, 20);
		
		text_pwd_1 = new Text(shell, SWT.BORDER);
		text_pwd_1.setBounds(171, 169, 187, 26);
		
		Label lab_tishi_pwd_1 = new Label(shell, SWT.WRAP);
		lab_tishi_pwd_1.setBounds(171, 201, 187, 20);
		text_pwd_1.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				String pwd_1 = text_pwd_1.getText().trim();
				//判空
				if(StringUtil.checkNull(pwd)) {
					return;
				}							
				//可以包含数字、字母、下划线，并且要同时含有数字和字母，且长度要在6-15位之间
				if(pwd.equals(pwd_1)) {
					lab_tishi_pwd_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
					lab_tishi_pwd_1.setText("密码正确");
				}else {
					lab_tishi_pwd_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
					lab_tishi_pwd_1.setText("密码错误");
				}
			}
		});

	}

}
